package com.gxuwz.stats.service;

import com.gxuwz.stats.service.impl.ExcelParser;
import com.gxuwz.stats.service.impl.PdfParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FileParserFactory {
    private final Map<String, FileParser> fileParsers = new HashMap<>();

    @Autowired
    public FileParserFactory(List<FileParser> parsers) {
        for (FileParser parser : parsers) {
            // 获取处理器类上的 @FileParserType 注解
            FileParserType annotation = parser.getClass().getAnnotation(FileParserType.class);
            if (annotation != null) {
                fileParsers.put(annotation.value(), parser);
            } else {
                throw new IllegalStateException("FileParser 缺少 @FileParserType 注解: " + parser.getClass());
            }
        }
    }

    public FileParser getParser(String fileType) {
        FileParser parser = fileParsers.get(fileType.toLowerCase());
        if (parser == null) {
            throw new IllegalArgumentException("Unsupported file type: " + fileType);
        }
        return parser;
    }
}